﻿#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->checkBoxTriple->setTristate(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_checkBox_toggled(bool checked)
{
    if(checked)
    {
        qDebug()<<tr("读书");
    }
    CheckHobbies(); //更新三态复选框
}


void MainWindow::on_checkBox_2_toggled(bool checked)
{
    if(checked)
    {
        qDebug()<<tr("爬山");
    }
    CheckHobbies(); //更新三态复选框
}


void MainWindow::on_checkBox_3_toggled(bool checked)
{
    if(checked)
    {
        qDebug()<<tr("游泳");
    }
    CheckHobbies(); //更新三态复选框

}


void MainWindow::on_checkBox_4_toggled(bool checked)
{
    if(checked)
    {
        qDebug()<<tr("发呆");
    }
    CheckHobbies(); //更新三态复选框
}


void MainWindow::on_checkBox_5_toggled(bool checked)
{
    if(checked)
    {
        qDebug()<<tr("逛街");
    }
    CheckHobbies(); //更新三态复选框
}


void MainWindow::on_checkBox_6_toggled(bool checked)
{
    if(checked)
    {
        qDebug()<<tr("吃货");
    }
    CheckHobbies(); //更新三态复选框
}


void MainWindow::on_pushButton_clicked()
{
    QString strResult = tr("兴趣爱好为：\r\n");
    if( ui->checkBox->isChecked() )
    {
        strResult += tr("读书\r\n");
    }
    if( ui->checkBox_2->isChecked() )
    {
        strResult += tr("爬山\r\n");
    }
    if( ui->checkBox_3->isChecked() )
    {
        strResult += tr("游泳\r\n");
    }
    if( ui->checkBox_4->isChecked() )
    {
        strResult += tr("发呆\r\n");
    }
    if( ui->checkBox_5->isChecked() )
    {
        strResult += tr("逛街\r\n");
    }
    if( ui->checkBox_6->isChecked() )
    {
        strResult += tr("吃货\r\n");
    }
    //弹窗显示
    QMessageBox::information(this, tr("兴趣爱好"), strResult);
}


void MainWindow::on_commandLinkButtonFile_clicked()
{
    QDesktopServices::openUrl( QUrl("file:///E:/QtProjects") );
}


void MainWindow::on_commandLinkButtonUrl_clicked()
{
    QDesktopServices::openUrl( QUrl("http://www.baidu.com") );
}

void MainWindow::CheckHobbies()
{
    int count = 0;  //兴趣计数
    if( ui->checkBox->isChecked() )
    {
        count++;
    }
    if( ui->checkBox_2->isChecked() )
    {
        count++;
    }
    if( ui->checkBox_3->isChecked() )
    {
        count++;
    }
    if( ui->checkBox_4->isChecked() )
    {
        count++;
    }
    if( ui->checkBox_5->isChecked() )
    {
        count++;
    }
    if( ui->checkBox_6->isChecked() )
    {
        count++;
    }
    //根据兴趣计数设置三态按钮的状态
    if( count <= 0)
    {
        ui->checkBoxTriple->setCheckState(Qt::Unchecked);
        return;
    }
    else if( count <= 5 )
    {
        ui->checkBoxTriple->setCheckState(Qt::PartiallyChecked);
        return;
    }
    else
    {
        ui->checkBoxTriple->setCheckState(Qt::Checked);
        return;
    }
}

